package com.jsnj33.pdaproject.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

import com.jsnj33.pdaproject.R;
import com.jsnj33.pdaproject.bean.JobBean;
import com.jsnj33.pdaproject.bean.ReasonBean;

import java.util.ArrayList;
import java.util.List;

public class JobAdapter extends RecyclerView.Adapter<JobAdapter.ViewHolder> {

    private Context mContext;
    private List<JobBean> list;
    private List<ReasonBean> list2;

    public JobAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_job, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        JobBean JobBean = list.get(position);
        holder.tv_os_no.setText(JobBean.getOs_no());
        holder.tv_rea_name.setText(JobBean.getRea_name());
        holder.tv_dep.setText(JobBean.getDep());
        holder.tv_qty.setText(JobBean.getQty() + "");
        holder.tv_smqty.setText(JobBean.getSmqty() + "");
        holder.tv_zc_time.setText(JobBean.getZc_time() + "");
        holder.tv_no_number.setText(JobBean.getYqty() + "");
        holder.tv_make_name.setText(JobBean.getPrd_name() + "");
        holder.tv_reason.setText(JobBean.getRem() + "");
        list.get(position).setRem(list2.get(position).getZC_Cause());

        holder.mSpinner.setAdapter(new ArrayAdapter(mContext,
                android.R.layout.simple_spinner_dropdown_item,
                list2));
        holder.mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                list.get(0).setRem(list2.get(position).getZC_Cause());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        RadioButton radioButton = (RadioButton) holder.mRgp.getChildAt(0);
        radioButton.setChecked(true);
        list.get(position).setMxp("0");
        holder.mRgp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rbFou:
                        list.get(position).setMxp("0");
                        break;
                    case R.id.rbShi:
                        list.get(position).setMxp("1");
                        break;
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }

    public void setList(List<JobBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public List<JobBean> getData() {
        return list;
    }

    public void setReasonList(ArrayList<ReasonBean> list2) {
        this.list2 = list2;
        notifyDataSetChanged();
    }

    public void clear() {
        if (this.list != null) {
            this.list.clear();
        }
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_os_no;
        private TextView tv_rea_name;
        private TextView tv_dep;
        private TextView tv_qty;
        private TextView tv_smqty;
        private TextView tv_zc_time;
        private TextView tv_no_number;
        private TextView tv_make_name;
        private TextView tv_reason;
        private Spinner mSpinner;
        RadioGroup mRgp;

        ViewHolder(View itemView) {
            super(itemView);
            tv_os_no = itemView.findViewById(R.id.tv_os_no);
            tv_rea_name = itemView.findViewById(R.id.tv_rea_name);
            tv_rea_name.setMovementMethod(new ScrollingMovementMethod());
            tv_dep = itemView.findViewById(R.id.tv_dep);
            tv_qty = itemView.findViewById(R.id.tv_qty);
            tv_make_name = itemView.findViewById(R.id.tv_make_name);
            tv_smqty = itemView.findViewById(R.id.tv_smqty);
            tv_zc_time = itemView.findViewById(R.id.tv_zc_time);
            tv_no_number = itemView.findViewById(R.id.tv_no_number);
            tv_reason = itemView.findViewById(R.id.tv_reason);
            mSpinner = itemView.findViewById(R.id.mSpinner);
            mRgp = itemView.findViewById(R.id.mRgp);
        }
    }


}

